﻿function updateCart(comicId, issue, condId, action) {
    var cart;
    var totalQty = 0;

    if ($.cookie("cart") == null)
        cart = {};
    else
        cart = $.parseJSON($.cookie("cart"));

    if (action == "+") {
        if (cart[comicId] == null || cart[comicId] == undefined)
            cart[comicId] = {};

        if (cart[comicId][issue] == null || cart[comicId][issue] == undefined)
            cart[comicId][issue] = {};

        if (cart[comicId][issue][condId] == null || cart[comicId][issue][condId] == undefined)
            cart[comicId][issue][condId] = 1;
        else
            cart[comicId][issue][condId] = parseInt(cart[comicId][issue][condId]) + 1;
    }
    else if (action == "-") {
        cart[comicId][issue][condId] = parseInt(cart[comicId][issue][condId]) - 1;
    }
    else if (action == "x") {
        delete cart[comicId][issue][condId];
    }
    else if (action == "xx")
        cart = {};

    $.cookie("cart", JSON.stringify(cart), { path: '/' });

    for (var comic in cart) {
        for (var issue in cart[comic]) {
            for (var condition in cart[comic][issue]) {
                totalQty += cart[comic][issue][condition];
            }
        }
    }

    if (cart != null)
        $("#cartQty").html(totalQty);
}